<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="commonLib.jsp"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/student-index.css'/>" />

<div class="container-fluid">
	<div class="row">
		<div class="col-xs-12 stuIndex-title">
			<div
				class="col-xs-12 col-sm-6 col-md-6 col-lg-3 stuIndex-title-left">
				<a href="${pageContext.request.contextPath}/lesson/studentCLesson">精准微课堂</a>
			</div>
			<div
				class="col-xs-12 col-sm-6 col-md-6 col-lg-3 col-lg-push-6 stuIndex-title-right">
				<img id="upload-img"
					src="<c:url value="/resource/picture/${image}" />" /> ${name}<a
					href="<c:url value='/user/update' />">[修改]</a><a
					href="<c:url value='/user/logout' />">[退出]</a> <input
					id="previewImg" type="file" class="form-control" name="file" >
					<input type="hidden" class="form-control" name="img-file" >
			</div>
		</div>
	</div>
</div>
<script>
	$("#upload-img").click(function(){
		document.getElementById("previewImg").click();
		$("#previewImg").change(function(){
		    var objUrl = getObjectURL(this.files[0]) ;
		    console.log("objUrl = "+objUrl) ;
		    if (objUrl) {
		        $("#upload-img").attr("src", objUrl) ;
// 		        alert($("#previewImg").val());
		    }
		    
		    ajaxFileUpload();
		}) ;
		function getObjectURL(file) {
		    var url = null ; 
		    if (window.createObjectURL!=undefined) { // basic
		        url = window.createObjectURL(file) ;
		    } else if (window.URL!=undefined) { // mozilla(firefox)
		        url = window.URL.createObjectURL(file) ;
		    } else if (window.webkitURL!=undefined) { // webkit or chrome
		        url = window.webkitURL.createObjectURL(file) ;
		    }
		    return url ;
		}
	});
	

	function ajaxFileUpload() {
		$.ajaxFileUpload({
			url : "<c:url value='/resource/upload'/>", //用于文件上传的服务器端请求地址
			secureuri : false, //是否需要安全协议，一般设置为false
			fileElementId : 'previewImg', //文件上传域的ID
			dataType : 'json', //返回值类型 一般设置为json
			success : function(data, status) //服务器成功响应处理函数
			{
				if(data.status=="success"){
					uploadImg(data.data.key);
				}else{
					alert(data.message);
				}
			},
			error : function(data, status, e)//服务器响应失败处理函数
			{
				alert(e);
			}
		})
		return false;
	}
	
	var uploadImg = function(key) {
		var json = {
			"picture":key
		}

		$.post("<c:url value='/user/changeAvatar'/>", {
			data : JSON.stringify(json)
		}, function(data) {
			if (data.status == "success") {
				location.reload();
			} else {
				alert(data.message);
			}
		});
	}
</script>